﻿using GalaSoft.MvvmLight.Messaging;
using MahApps.Metro.Controls;
using System;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using Transitionals;

namespace BingWallPaper
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        private ObservableCollection<Type> transitionTypes = new ObservableCollection<Type>();
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoadTransitions(Assembly.GetAssembly(typeof(Transition)));
            Messenger.Default.Register<SetContent>(this, c =>
            {
                setTrans();
                mainp.Content = c.Image;
            });
        }

        void setTrans()
        {
            int m_nIndex = 0;
            m_nIndex = new Random().Next(0, transitionTypes.Count);
            Type transitionType = transitionTypes[m_nIndex];
            Transition t = (Transition)Activator.CreateInstance(transitionType);

            mainp.Transition = t;
        }
        public void LoadTransitions(Assembly assembly)
        {
            foreach (Type type in assembly.GetTypes())
            {
                // Must not already exist
                if (transitionTypes.Contains(type)) { continue; }

                // Must not be abstract.
                if ((typeof(Transition).IsAssignableFrom(type)) && (!type.IsAbstract))
                {
                    transitionTypes.Add(type);
                }
            }
        }
    }
}
